<?php
/*****************************************
 This file is part of the Dynamo Core CMS  
 Copyright (c) Dynamo Studios 2008
 
 *** NOTE ***
 Permission is hereby granted to execute this file as a
 web service under the terms of the license agreement.
 
 See LICENSE.txt, in the system folder.
 
All other rights, other than those expressed in the license
document, are reserved.

*************
Query Services (QS)

 ****************************************/

 
class cDycmsAbstractQuery extends cDbQuery implements IDycmsObject
{
	public $mObject;
	
	function __construct()
	{
		parent::__construct();
		$this->mObject = NULL;
	}
	
	/**
	 * Add an array of criteria for the query.
	 **/
	public function AddArray($vz)
	{
		if(!is_array($vz))
			return false;
			
		$this->mCriteria = array_merge($this->mCriteria, $vz);
		
		return true;
	}
	
	function GetClsId($i = -1)
	{
		return $this->mObject->GetClsId($i = -1);
	}
	
	function IsCollection()
	{
		return $this->mObject->IsCollection();
	}
	
	function Edit(IDycmsClient &$r_client)
	{
		return $this->mObject->Edit($r_client);
	}
	
	function GetId($i = -1)
	{
		return $this->mObject->GetId($i);
	}
	
	function Get($name, $i = -1)
	{
		return $this->mObject->Get($name, $i);
	}
	function GetOwner($i = -1){
		return $this->mObject->GetOwner($i);
	}
	
	function Render(cDycmsClientNode &$r_client, $i = -1)
	{
		return $this->mObject->Render($r_client, $i);
	}
	
	public function Count()
	{
		return $this->mObject->Count();
	}
	
	function GetUrl($i = -1){
		return $this->mObject->GetUrl($i);
	}
	
	function GetSummary($i = -1)
	{
		return $this->mObject->GetSummary($i);
	}
	
	function GetFilteredIndexValues($idx, $argv, $assoc=null) {
		if($this->mObject->IsCollection())
			return $this->mObject->GetFilteredIndexValues($idx, $argv, $assoc);
	}
}


class cDycmsQuery extends cDycmsAbstractQuery
{
	protected $mClassId; /** Deprecated **/
	protected $msQueryString;
	
	public function __construct() {
		parent::__construct();
	}
	
	public function Execute($cat = true, $idxonly = false)
	{
		$vzz_obj = null;	// Object result set
		
		dyLog("QS: Executing query");
		
		//
		// Execute the query
		//
		
		foreach($this->mCriteria as $s_idx => $s_val)
		{
			//
			// TODO: format value
			//
			
			
			//
			// Perform index query on our set (null set for first iteration)
			//
			
			$vzz_obj = dbReadByIndex($s_idx, $s_val, $vzz_obj);
		};
		
		
		//
		// Load the collection
		//
		
		$this->mObject = null;
		if(count($vzz_obj) == 1)
		{
			//
			// Only one object in result set - load it
			//
			
			$objcls = &dyLoadClass($vzz_obj[0]['$cnum']);
			$this->mObject = &$objcls->LoadObject($vzz_obj[0]['$id']);
		}
		else if($this->mClassId)
		{
			//
			// Create a class-specific collection
			//
			
			dyLoadClassById($this->mClassId);
			$cls = "cCollection".$this->mClassId;
			if(class_exists($cls))
				$this->mObject = new ${cls}($vzz_obj, $this->mCriteria, array(
					'noCat' => !$cat
				));
		}
		if($this->mObject == null)
		{
			//
			// Create the collection
			//
			
			$this->mObject = new cDycmsCollection($vzz_obj, $this->mCriteria, array(
				'noCat' => !$cat
			));
		};
		
		dyLog("QS: Query completed");
		return $this->mObject->Count();
	}
	
	public function SetClass($clsid)
	{
		$this->mClassId = $clsid;
	}
	
	public function Flush()
	{
		$this->mObject = NULL;
		$this->mClassId = NULL;
		$this->mCriteria = array();
	}
}

class cDycmsObjectQuery extends cDycmsAbstractQuery
{
	protected $mClassId;
	
	public function __construct($clsid) {
		parent::__construct();
		$this->mClassId = $clsid;
	}
	
	public function Execute($cat = true, $forceCol = false)
	{
		$vzz_obj = null;	// Object result set
		
		dyLog("QS: Executing query");
		
		//
		// Execute the query
		//
		
		$vzz_obj = $this->DoObjectQuery($this->mClassId);
		
		
		//
		// Load the collection
		//
		
		if((count($vzz_obj) == 1) && !$forceCol)
		{
			//
			// Only one object in result set - load it
			//
			
			$objcls = &dyLoadClassById($this->mClassId);
			$this->mObject = &$objcls->LoadObject($vzz_obj[0]['$id']);
		}
		else
		{
			//
			// Create a class-specific collection
			//
			
			dyLoadClassById($this->mClassId);
			$cls = "cCollection".$this->mClassId;
			if(class_exists($cls))
				$this->mObject = new ${cls}($vzz_obj, $this->mCriteria, array(
					'objectQuery' => true, 'classId' => $this->mClassId
				));
			else
			{
				//
				// Create the collection
				//
				
				$this->mObject = new cDycmsCollection($vzz_obj, $this->mCriteria, array(
					'objectQuery' => true, 'classId' => $this->mClassId
				));
			}
		};
		
		dyLog("QS: Query completed");
		return $this->mObject->Count();
	}
}

function dyCompareRsItem($z_val, $z_item)
{
	return (($z_val['$cnum'] == $z_item['$cnum'])
			&&
			($z_val['$id']) == $z_item['$id']);
}

function dyFindRsItem($zz_rs, $z_item)
{
	foreach ($zz_rs as $key => $z_val)
	{
		if(dyCompareRsItem($z_val, $z_item))
			return $key;
	}
	
	return false;
}

?>